<?php


class Teacher_Controller extends BaseController {
	public function index(Request $request, Response $response) {
		$teacher = Teacher::load($this->user->internalId);
		$response->append(new View('teacher/personalInfo', compact('teacher')));
	}

	//授课班列表显示
	public function courseClasses(Request $request, Response $response) {
		$teacher = Teacher::load($this->user->internalId);
		$courseClasses = CourseClass::find(new Query('teacherId', $teacher->id), ['nolimit' => 1]);
		$classTimes = [];
		foreach ($courseClasses as $courseClass) $classTimes[$courseClass->id] = ClassTime::find(new Query('classId', $courseClass->id), ['nolimit' => 1]);
		if (!$courseClasses) {
			$courseClasses = array();
		}
		$response->append(new View('teacher/courseClasses', compact('courseClasses', 'classTimes')));
	}

	//单个课程班学生成绩管理
	public function scoreList(Request $request, Response $response) {
		$courseClassId = $request->get('courseClassId');
		$courseSelections = CourseSelection::find(new Query('courseClassId', $courseClassId), ['nolimit' => 1]);
		if (!$courseSelections) {
			$courseSelections = array();
		}
		$response->append(new View('teacher/scoreList', compact('courseSelections', 'courseClassId')));
	}

	//成绩编辑
	public function scoreEdit(Request $request, Response $response) {
		$courseClassId = $request->get('courseClassId');
		$courseSelections = CourseSelection::find(new Query('courseClassId', $courseClassId), ['nolimit' => 1]);
		$response->append(new View('teacher/scoreEdit', compact('courseClassId', 'courseSelections')));
	}

	//学生成绩录入
	public function update(Request $request, Response $response) {
		$courseClassId = $request->post('courseClassId');
		$courseSelections = CourseSelection::find(new Query('courseClassId', $courseClassId), ['nolimit' => 1]);
		foreach ($courseSelections as $courseSelection) {
			$courseSelection->result = $request->post($courseSelection->studentId);
			$courseSelection->save();
		};
		$response->alertAndGoTo('成绩修改成功！', '/teacher/courseClasses/');
	}
}